/*
 * @Author: xuhongling
 * @Date:   2023-08-27 10:51:00
 * @Last Modified by:   xuhongling
 * @Last Modified time: 2023-08-27 11:23:31
 */
import dayjs from 'dayjs';
import { Hformat } from '@/hooks/web/useHQPFormat';

// ECharts Option
const setChartOption = (chartData, selectedYears, stationData) => {
  const timeData: any = [];
  const rainData: any = [];
  /*for (const year in chartData) {
    chartDataObj[year] = [];
    for (let i = 0; i < chartData[year].length; i++) {
      chartDataObj[year].push(chartData[year][i]['accp']);
      if (!chartDataObj.time) {
        chartDataObj.time = [];
      }
      chartDataObj.time.push(dayjs(chartData[year][i].tmMin).format('MM-DD'));
      chartDataObj.time = Array.from(new Set(chartDataObj.time));
    }
  };*/
  for (let i = 0; i < chartData.length; i++) {
    timeData.push(chartData[i].year);
    rainData.push(chartData[i].accp);
  }
  const chartOption = {
    color: ['#5470c6', '#298def', '#91cc75'],
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      left: "center",
      top: "44",
      itemWidth: 20,
      data:['雨量'],
    },
    xAxis: {
      type: 'category',
      boundaryGap: true,
      data: timeData,
      axisLine: {
        onZero: false,
        lineStyle: {
          type: 'solid',
          color: '#333'
        }
      },
      axisTick: {
        show: true,
        inside: true,
        interval: 'auto',
        lineStyle: {
          color: '#999'
        }
      },
      axisLabel: {
        show: true,
        margin: 14,
        textStyle: {
          color: "#333",
          fontSize: 12,
        },
      },
    },
    yAxis: [
      {
        type: 'value',
        position: 'left',
        name: '降雨量(mm)',
        scale: true,
        splitNumber: 5,
        boundaryGap: ['0', '5%'],
        max: (value)=> {
          if (value.max < 5) {
            return 10
          }else if (value.max >= 5 && value.max < 10){
            return 15
          }else {
            return value.max + 3
          }
        },
        nameTextStyle: {
          color: '#333'
        },
        axisTick: {
          show: false,
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: '#333'
          }
        },
        axisLabel: {
          show: true,
          textStyle: {
            color: "#333",
            fontSize: 12,
          }
        },
        splitLine: {
          show: true,
          lineStyle:{
            type: 'dashed',
            color:'#f4f4f4'
          }
        },
      }
    ],
    series: [
      {
        name: '雨量',
        type: 'bar',
        yAxisIndex: 0,
        barMaxWidth: 10, //柱图宽度
        lineStyle: {
          color: '#298def',
          width: 2
        },
        data: rainData
      },
    ],
  }
  return chartOption;
};

export default setChartOption;
